﻿@using Stamp_For_Africa_Version_2.Models
@using Stamp_For_Africa_Version_2.ViewModels
@model Stamp_For_Africa_Version_2.ViewModels.AlbumPageViewModel

<script type="text/javascript">
  $(document).ready(function () {

    $(".albumList").sortable({
      connectWith: '.albumList',
      accept: '.albumList',
      opacity: 0.4,
      tolerance: 'pointer',
      placeholder: 'ui-state-highlight',
      revert: true,
      helper: "clone",
      forcePlaceholderSize: true,
      activeClass: "ui-state-highlight",
      receive: function (event, ui) {
        var $item = $(this);
        var albumId = ui.item.find("p").attr("albumid");
        var pageNumber = $('#CurrentSelectedPage').val();
        var stampId = ui.item.find("p").attr("stampid");
        var origin = ui.item.find("p").attr("origin");

        if (origin == "collection") {
          ui.item.find("p").attr("origin", "album");
          addStampToAlbum(albumId, stampId, pageNumber);
        }
        else {
          ui.item.find("p").attr("origin", "collection");
        }
      },
      remove: function (event, ui) {

        var $item = $(this);
        var albumId = ui.item.find("p").attr("albumid");
        var pageNumber = $('#CurrentSelectedPage').val();
        var stampId = ui.item.find("p").attr("stampid");
        var origin = ui.item.find("p").attr("origin");
        var collectionId = ui.item.find("p").attr("collectionid");
        var pageId = "#albumPage" + @Model.PageNumber;
        var qIndicator = "#quantityIndicator" + collectionId;
        if (origin == "album") {
          removeStampFromAlbum(albumId, stampId);
        }
        else {
          var quantity = ui.item.find("p").attr("quantity");
          quantity = quantity - 1;
          if (quantity > 0)
          {
            ui.item.find(qIndicator).text(quantity);
            var clone = ui.item.clone();
            clone.appendTo($('#MintList'));
            ui.item.find(qIndicator).text(0);
            
            
          }

          //$(qIndicator).text(quantity);
        }


      }
    }).disableSelection();


    // image preview function, demonstrating the ui.dialog used as a modal window
    function viewLargerImage(dlgId, title) {
      var jq = '#' + dlgId;
      $(jq).dialog({ width: 400, title: title, modal: true });
      return false;
    }




  });

</script>
@{
  var pageId = "albumPage" + Model.PageNumber;
 <ul id=@pageId class="albumList ui-helper-reset ui-helper-clearfix" pagenumber="@Model.PageNumber">
 @{
    foreach (var stamp in Model.Stamps)
    {
			Collection collection = new Collection();
      collection.Id = Guid.NewGuid();
      collection.Stamp = stamp;
      
      @Html.Partial("_AlbumEntry", new AlbumEntryViewModel { AlbumId = Model.Album.Id.ToString(), Origin = "album", Collection = collection })
    }
  }
</ul>
  }

